-----------------------------------------
-- The price coefficient for "used" units
-----------------------------------------
-- Change this to the desired value. The resulting value will be rounded down to the nearest integer.
local TRAINING_CAMP_LAYOFF_COEFF = 0.5
---------------------------------------------
-- The price coefficient for "upgraded" units
---------------------------------------------
-- Change this to the desired value. The resulting value will be rounded down to the nearest integer.
local TRAINING_CAMP_UPGRADE_COEFF = 1.25

local success = false
local hero_money_new = 0
local unit_count_original = 0
local unit_count_upgraded = 0
local complete_army_new
local unit_upgrade_choice = 0

--show dialogs
function get_army_upgrade_info()
	local army_atom,army_count = get_army_table(Logic.hero_lu_army(),5)
	local shop_info = ""
	for i in pairs(army_atom) do
		if army_count[i]>0 then
		    if can_unit_be_upgraded(army_atom[i]) then
			    local unit_type_new, price_original, price_upgraded = get_upgrade_path(army_atom[i])
				local army_desc = "<imu="..army_atom[i]..".png><label=cpn_"..army_atom[i].."> --->>> <imu="..unit_type_new..".png><label=cpn_"..unit_type_new.."><br>"
                shop_info = shop_info..army_desc
			end
		end
	end
	return shop_info
end

function get_army_upgrade_count()
	local army_atom,army_count = get_army_table(Logic.hero_lu_army(),5)
	local label_info = ""
	local label_tmp = ""
	for i in pairs(army_atom) do
		if army_count[i]>0 then
		    if can_unit_be_upgraded(army_atom[i]) then
				local unit_type_upgraded, unit_count_upgraded, cost_of_upgrade = training_camp_new_army(army_atom[i],army_count[i])
				local label_tmp = "<label=cpn_"..army_atom[i]..">("..army_count[i]..") --->>> <label=cpn_"..unit_type_upgraded..">(<color=225,0,0>"..unit_count_upgraded.."</color>)<br>"
                label_info = label_info..label_tmp
			end
		end
	end
	return label_info
end

function get_army_upgrade_exit()
	local army_atom,army_count = get_army_table(Logic.hero_lu_army(),5)
	local no_upgrade = false
	for i in pairs(army_atom) do
		if army_count[i]>0 then
			if can_unit_be_upgraded(army_atom[i]) then
				no_upgrade = true
			end
		end
	end
	if no_upgrade then
	    return "<label=dlg_exit>"
	else
		return "<label=no_army_can_upgrade>"
	end
end

function get_army_upgrade_detail()
    local army_atom,army_count = get_army_table(Logic.hero_lu_army(),5)
	local unit_type_new, price_original, price_upgraded = get_upgrade_path(army_atom[unit_upgrade_choice])
	local unit_type_original = army_atom[unit_upgrade_choice]
	local label
	if hero_money_new < 0 then
       label = "<label=money_not_enough>"
    elseif unit_count_original > unit_count_upgraded then
	   label = "<label=army_upgrade3>"..unit_count_upgraded.."<label=num><label=cpn_"..unit_type_original.."><label=army_upgrade1><label=cpn_"..unit_type_new..">"
	   label = label.."<br><label=army_upgrade2>"..unit_count_original.."<label=num><label=cpn_"..unit_type_original.."><label=army_upgrade1>"..unit_count_upgraded.."<label=num><label=cpn_"..unit_type_new.."> ?"
	else
	   label = "<label=army_upgrade2><br>".."<label=cpn_"..unit_type_original.."><imu="..unit_type_original..".png><label=army_upgrade1><label=cpn_"..unit_type_new.."><imu="..unit_type_new..".png>".." ?"
    end
	return label
end

function get_army_upgrade_type(pos)
    local army_atom,army_count = get_army_table(Logic.hero_lu_army(),5)
	local label= "<label=cpn_"..army_atom[pos]..">"
	return label

end

function get_army_upgrade_type1()
   return get_army_upgrade_type(1)
end

function get_army_upgrade_type2()
   return get_army_upgrade_type(2)
end

function get_army_upgrade_type3()
   return get_army_upgrade_type(3)
end

function get_army_upgrade_type4()
   return get_army_upgrade_type(4)
end

function get_army_upgrade_type5()
   return get_army_upgrade_type(5)
end

----------------------------------------------------------------
-- Determine how the army will be upgraded (unit, count, price).
----------------------------------------------------------------
function training_camp_new_army(unit_type_original, unit_count_original)
	local unit_price_original=0
	local unit_price_upgraded=0
	-- These will be the returned values
	local unit_type_upgraded
	local unit_count_upgraded=0
	local cost_of_upgrade=0

	-- Detemine the price and the army type for the upgrade
	unit_type_upgraded, unit_price_original, unit_price_upgraded = get_upgrade_path (unit_type_original)
	-- Detemine the leadership requirement for the army and its upgrade
	local leadership_original = Logic.cp_leadship (unit_type_original)
	local leadership_upgraded = Logic.cp_leadship (unit_type_upgraded)
    local leadership_hero = Logic.hero_lu_item("leadership","count")
	-- Detemine the level of the army and its upgrade (needed for cost calculation)
	local level_original = Logic.cp_level(unit_type_original)
	local level_upgraded = Logic.cp_level(unit_type_upgraded)

	local unit_count_upgraded_total = get_unit_total_count(unit_type_upgraded)
	-- Determine the amount of upgraded units
    if (unit_count_original+unit_count_upgraded_total)*leadership_upgraded > leadership_hero then
		unit_count_upgraded = math.floor (leadership_hero/leadership_upgraded) - unit_count_upgraded_total
    else
	    unit_count_upgraded = unit_count_original
	end
	-- Determine the value of the army and its upgrade
	local value_original = math.ceil(unit_price_original*unit_count_original*TRAINING_CAMP_LAYOFF_COEFF) -- Old troops worth less
	local value_upgraded = math.ceil(unit_price_upgraded*unit_count_upgraded*TRAINING_CAMP_UPGRADE_COEFF) -- New troops cost more
	-- Determine the price to be paid for the upgrade
	cost_of_upgrade=(value_upgraded-value_original)*(1+level_upgraded-level_original)
	return unit_type_upgraded, unit_count_upgraded, cost_of_upgrade
end

function training_camp_layoff_coeff_percent()
	local label = tostring(TRAINING_CAMP_LAYOFF_COEFF*100)
	return label
end

function training_camp_upgrade_coeff_percent()
	local label = tostring(((TRAINING_CAMP_UPGRADE_COEFF-1)*100))
	return label
end

-------------------------------------
-- Upgrade army in the indicated slot
-------------------------------------
function training_camp_upgrade(slotnumber)
	success = false -- Flag to signal successful transaction
	-- Sanity check
	if slotnumber >= 1 and slotnumber <=5 then
		-- Get amount of gold
		local hero_gold_amount=get_gold_count()
		-- Get the army of the hero
		local complete_army = Logic.hero_lu_army()

		-- Backup the entire army (all 7 slots - they will be needed later on)
		local unit_type_in_slot1, unit_count_in_slot1 = Logic.army_get(complete_army, 1)
		local unit_type_in_slot2, unit_count_in_slot2 = Logic.army_get(complete_army, 2)
		local unit_type_in_slot3, unit_count_in_slot3 = Logic.army_get(complete_army, 3)
		local unit_type_in_slot4, unit_count_in_slot4 = Logic.army_get(complete_army, 4)
		local unit_type_in_slot5, unit_count_in_slot5 = Logic.army_get(complete_army, 5)
		local unit_type_in_slot6, unit_count_in_slot6 = Logic.army_get(complete_army, 6)
		local unit_type_in_slot7, unit_count_in_slot7 = Logic.army_get(complete_army, 7)

		-- Take an extra copy for the slot we are working with
		unit_type_original, unit_count_original = Logic.army_get(complete_army, slotnumber)
		-- See if the selected slot is valid (there is at least one unit that can be upgraded)
		if unit_count_original > 0 and can_unit_be_upgraded(unit_type_original) then
			-- Get upgrade info
			unit_type_upgraded, unit_count_upgraded, cost_of_upgrade = training_camp_new_army(unit_type_original, unit_count_original)
			hero_money_new = hero_gold_amount - cost_of_upgrade
			-- Check that the hero can afford the army.
			if hero_money_new >= 0 then
				-- Ok, hero can afford the upgrade.
				-- Check that after the upgrade the number of units would be greater than zero.
				if unit_count_upgraded > 0 then
					-- Ok, the result will be more than zero.
					-- Check which slot was selected to be upgraded, and create the appropriate army.
					if slotnumber==1 then
						complete_army_new=unit_type_upgraded .."|".. unit_count_upgraded.."|"..unit_type_in_slot2.."|"..unit_count_in_slot2.."|"..unit_type_in_slot3.."|"..unit_count_in_slot3.."|"..unit_type_in_slot4.."|"..unit_count_in_slot4.."|"..unit_type_in_slot5.."|"..unit_count_in_slot5.."|"..unit_type_in_slot6.."|"..unit_count_in_slot6.."|"..unit_type_in_slot7.."|"..unit_count_in_slot7
					elseif slotnumber==2 then
						complete_army_new=unit_type_in_slot1 .."|".. unit_count_in_slot1.."|"..unit_type_upgraded.."|"..unit_count_upgraded.."|"..unit_type_in_slot3.."|"..unit_count_in_slot3.."|"..unit_type_in_slot4.."|"..unit_count_in_slot4.."|"..unit_type_in_slot5.."|"..unit_count_in_slot5.."|"..unit_type_in_slot6.."|"..unit_count_in_slot6.."|"..unit_type_in_slot7.."|"..unit_count_in_slot7
					elseif slotnumber==3 then
						complete_army_new=unit_type_in_slot1 .."|".. unit_count_in_slot1.."|"..unit_type_in_slot2.."|"..unit_count_in_slot2.."|"..unit_type_upgraded.."|"..unit_count_upgraded.."|"..unit_type_in_slot4.."|"..unit_count_in_slot4.."|"..unit_type_in_slot5.."|"..unit_count_in_slot5.."|"..unit_type_in_slot6.."|"..unit_count_in_slot6.."|"..unit_type_in_slot7.."|"..unit_count_in_slot7
					elseif slotnumber==4 then
						complete_army_new=unit_type_in_slot1 .."|".. unit_count_in_slot1.."|"..unit_type_in_slot2.."|"..unit_count_in_slot2.."|"..unit_type_in_slot3.."|"..unit_count_in_slot3.."|"..unit_type_upgraded.."|"..unit_count_upgraded.."|"..unit_type_in_slot5.."|"..unit_count_in_slot5.."|"..unit_type_in_slot6.."|"..unit_count_in_slot6.."|"..unit_type_in_slot7.."|"..unit_count_in_slot7
					elseif slotnumber==5 then
						complete_army_new=unit_type_in_slot1 .."|".. unit_count_in_slot1.."|"..unit_type_in_slot2.."|"..unit_count_in_slot2.."|"..unit_type_in_slot3.."|"..unit_count_in_slot3.."|"..unit_type_in_slot4.."|"..unit_count_in_slot4.."|"..unit_type_upgraded.."|"..unit_count_upgraded.."|"..unit_type_in_slot6.."|"..unit_count_in_slot6.."|"..unit_type_in_slot7.."|"..unit_count_in_slot7
					end
					success = true
                end
			end
		end
	end
end

-------------------------------------------------------
-- Determine whether we show a choice to upgrade slot n
-------------------------------------------------------
function training_camp_dlg_condition(n)
    local success=false
	Logic.hero_lu_var("training_camp_dlg",0)
	local army_atom,army_count = get_army_table(Logic.hero_lu_army(),5)
	if can_unit_be_upgraded(army_atom[n]) then
	     success = true
	end
	if success then
	   Logic.hero_lu_var("training_camp_dlg",n)
	end
	return success
end

-------------------------------------------------------
-- Determine whether we show a choice to upgrade slot 1
-------------------------------------------------------
function training_camp_dlg_condition1(param) --ftag:if
	return training_camp_dlg_condition(1)
end

-------------------------------------------------------
-- Determine whether we show a choice to upgrade slot 2
-------------------------------------------------------
function training_camp_dlg_condition2(param) --ftag:if
	return training_camp_dlg_condition(2)
end

-------------------------------------------------------
-- Determine whether we show a choice to upgrade slot 3
-------------------------------------------------------
function training_camp_dlg_condition3(param) --ftag:if
	return training_camp_dlg_condition(3)
end

-------------------------------------------------------
-- Determine whether we show a choice to upgrade slot 4
-------------------------------------------------------
function training_camp_dlg_condition4(param) --ftag:if
	return training_camp_dlg_condition(4)
end

-------------------------------------------------------
-- Determine whether we show a choice to upgrade slot 5
-------------------------------------------------------
function training_camp_dlg_condition5(param) --ftag:if
	return training_camp_dlg_condition(5)
end

function training_camp_dlg_use(x)
    unit_upgrade_choice = x
    training_camp_upgrade(x)
	return true
end

------------------------------------------------------
-- User chose to upgrade slot #1. Store the selection.
------------------------------------------------------
function training_camp_dlg_use1(param) --ftag:action
	return training_camp_dlg_use(1)
end

------------------------------------------------------
-- User chose to upgrade slot #2. Store the selection.
------------------------------------------------------
function training_camp_dlg_use2(param) --ftag:action
	return training_camp_dlg_use(2)
end

------------------------------------------------------
-- User chose to upgrade slot #3. Store the selection.
------------------------------------------------------
function training_camp_dlg_use3(param) --ftag:action
	return training_camp_dlg_use(3)
end

------------------------------------------------------
-- User chose to upgrade slot #4. Store the selection.
------------------------------------------------------
function training_camp_dlg_use4(param) --ftag:action
	return training_camp_dlg_use(4)
end

------------------------------------------------------
-- User chose to upgrade slot #5. Store the selection.
------------------------------------------------------
function training_camp_dlg_use5(param) --ftag:action
	return training_camp_dlg_use(5)
end

function training_camp_dlg_choice(param)
	if success then
		-- Give the new army to the hero
		Logic.hero_lu_army(complete_army_new)
	    -- Take the money from the hero
	    Logic.hero_lu_item("money", "count", hero_money_new)
	end
	return true
end
